<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-container" >
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" >
        <!-- <el-form-item label="账号" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="请输入账号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="创建时间">
              <el-date-picker
                v-model="queryParams.createTime"
                :editable="false"
                type="daterange"
                range-separator="~"
                start-placeholder="开始时间"
                end-placeholder="截止时间"
                value-format="YYYY-MM-DD"
              />
            </el-form-item> -->

        <el-form-item label="查询对象" prop="operateType">
          <el-select
            v-model="queryParams.operateType"
            placeholder="全部"
            clearable
            style="width: 150px"
          >
          
            <el-option :label="'全部'" :value="''" />
            <el-option :label="'个人'" :value="'1'" />
            <el-option :label="'团队'" :value="'2'" />

          </el-select>
        </el-form-item>    

        <el-form-item class="search-buttons">
          <el-button class="filter-item" type="primary" icon="search" @click="handleQuery">
            搜索
          </el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>


          <el-button icon="info" style="margin-left: 150px;">团队总充值:{{ totalRec }}</el-button>
          <el-button icon="info" style="margin-left: 20px;">团队今日充值:{{ todayRec }}</el-button>
          <el-button icon="info" style="margin-left: 20px;">团队昨日充值:{{ yesterdayRec }}</el-button>

         



        </el-form-item>
      </el-form>
    </div>
  
    <el-card shadow="hover" class="data-table">
      <el-table
        v-loading="loading"
        :data="Lists"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        class="data-table__content"
        @selection-change="handleSelectionChange"
      >

        <el-table-column prop="Id" label="序号" width="120" />
        <el-table-column prop="userInfo.account" label="用户账号" />
        <el-table-column prop="userInfo.nickname" label="用户昵称" />
        <el-table-column prop="userInfo.markId" label="邀请码" />
        <el-table-column prop="fromAddress" label="转账地址" />
        <el-table-column prop="toAddress" label="接收地址" />
        <el-table-column prop="amount" label="转账金额" />
        <el-table-column prop="hash" label="转账哈希" />
        <el-table-column prop="status" label="归集状态" width="120">
          <template #default="scope">
            <span v-if="scope.row.status === 0">待处理</span>
            <span v-else-if="scope.row.status === 1">成功</span>
            <span v-else>失败</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="归集备注" />

        <el-table-column prop="type" label="币种" width="120">
          <template #default="scope">
            <span v-if="scope.row.type === 0">波场</span>
            <span v-else>币安</span>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" width="180">

        </el-table-column>

        <el-table-column v-if="false" label="操作" fixed="right" align="left" width="200">
          <template #default="scope">
            
            <el-button
              v-hasPerm="['finance:charge:query']"
              type="primary"
              link
              size="small"
              icon="edit"
              @click.stop="handleOpenDialog(scope.row.id,scope.row)"
            >
              编辑
            </el-button>
            
          </template>
        </el-table-column>
      </el-table>

       <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="handleQuery"
          />
    </el-card>

    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="600px"
      @closed="handleCloseDialog"
    >
      <el-form ref="FormRef" :model="formData" :rules="rules" label-width="120px">
        
        <el-form-item label="状态" >
          <el-radio-group v-model="formData.status">
            <el-radio :value="1">已完成</el-radio>
            <el-radio :value="2">已失败</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "充值记录",
  inheritAttrs: false,
});

import FinanceAPI, { ChargeVO } from "@/api/system/finance.api";

const props = defineProps({
  id: {
    type: [String, Number],
    default: 0,
  },
  account: {
    type: String,
    default: "",
  },
});
const id = props.id
const account = props.account
console.log("id:", id, "account:", account);



const queryFormRef = ref();
const FormRef = ref();

const loading = ref(false);
const selectIds = ref<number[]>([]);

const queryParams = reactive<any>({
  pageNum: 1,
  pageSize: 10,
  operateType: "",
});

const totalRec = ref(0);
const todayRec = ref(0);
const yesterdayRec = ref(0);

const total = ref(0);

const dialog = reactive({
  title: "",
  visible: false,
});

const Lists = ref<ChargeVO[]>();
const formData = reactive<any>({
  id: "",
  status: 1,
  auditTime: "",
});

const rules = reactive({
  status: [{ required: true, message: "请选择状态", trigger: "change" }],
});

// 查询
function handleQuery() {

  const json = {
    page: queryParams.pageNum || 1,
    limit: queryParams.pageSize|| 10,
    userId: id || "",
    operateType:queryParams.operateType || "",
  };
  console.log(queryParams)
  loading.value = true;

  FinanceAPI.List(json)
    .then((res) => {
      Lists.value = res.list.data;
      total.value = res.list.total || 0;
      totalRec.value = res.team.totalRec || 0;
      todayRec.value = res.team.todayRec || 0;
      yesterdayRec.value = res.team.yesterdayRec || 0;
      console.log("查询结果:", res);
    })
    .catch((error) => {
      console.error("查询失败:", error);
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}

// 处理选中项变化
function handleSelectionChange(selection: any) {
  selectIds.value = selection.map((item: any) => item.id);
}



/**
 * 打开新增弹窗
 *
 * @param Id ID
 */
async function handleOpenDialog(Id?:string,data?:ChargeVO) {

  dialog.visible = true;
  if (Id) {
    dialog.title = "修改";
    Object.assign(formData, data);
  } else {
    dialog.title = "新增";
  }
}

// 提交表单
function handleSubmit() {
  FormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      
      ElMessage.success("修改成功");
      // FinanceAPI.update(formData.id, formData)
      //   .then(() => {
      //     ElMessage.success("修改成功");
      //     handleCloseDialog();
      //     handleQuery();
      //   })
      //   .finally(() => (loading.value = false));
      
      handleCloseDialog();
      handleQuery();
      loading.value = false
    }
  });
}



// 重置表单
function resetForm() {
  FormRef.value.resetFields();
  FormRef.value.clearValidate();
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  resetForm();
}

onMounted(() => {
  handleQuery();
});
</script>
